import EventEmitter from "events";
import crc from 'crc'
const generateCrcHex = (id: string): string => { 
    const head = 'f7f7'
    const tail = '7f7f'
    let noCrc:string = id + '0380010b'
    const crc16 = crc.crc16modbus(Buffer.from(noCrc,'hex'))
    return head + noCrc + crc16.toString(16).padStart(4,'0') + tail
}
class EventController extends EventEmitter {
    public moduleNum:number = 6; //模块的数量
    public settingData:Array<Array<Record<string,string | number>>> = []
    public flog: boolean = true;
    public pre_time = new Date().getTime()
    public current_module_id: number = 1 // 当前叫号的模块
    public initModuleList(moduleNum: number) {
        for(let i=0;i<this.moduleNum;i++) {
            this.settingData.push([
            {
                id: 1,
                name: "message.uv",
                normal_low: 100,
                normal_high: 5000,
                current_val: 1300,
            },
            {
                id: 2,
                name: "message.inf",
                normal_low: -40,
                normal_high: 150,
                current_val: 100
            },
            {
                id: 3,
                name: "message.tem",
                normal_low: -40,
                normal_high: 80,
                current_val: 50
            },
            {
                id: 5,
                name: "message.hum",
                normal_low: 0,
                normal_high: 90,
                current_val: 75
            },
            {
                id: 6,
                name: "message.pre",
                normal_low: 0,
                normal_high: 200,
                current_val: 121
            }
         ])
        }
    }
    // 初始化模块的个数
    public initConfig(moduleNum: number) {
        if (isNaN(moduleNum) && this.moduleNum > 10) {
            console.log("设置配置成功" + moduleNum)
            this.moduleNum = 10
            this.initModuleList(this.moduleNum)
        } else {
            this.moduleNum = moduleNum
            this.initModuleList(this.moduleNum)
        }
    
    }
    // 修改阈值
    changeVal(id,type,val) {
        try {
            this.settingData[id - 1][type].current_val = val // 设置报警阈值
        } catch (error) {
            console.log(error)
        }
    }

}
const eventEmit = new EventController()

// 开启轮询叫号 
// 有延时的轮询
eventEmit.on("polling_call",(serialport)=>{
    let timer: number = 20
    timer = eventEmit.moduleNum > 10? 20 : 200/eventEmit.moduleNum
    setInterval(()=> {
        polling_call(serialport)
    },timer)
})
function polling_call(serialport) {
    if (eventEmit.current_module_id > eventEmit.moduleNum) {
        eventEmit.current_module_id = 1
    } 
    let id:any = null
    if (eventEmit.current_module_id <10) id = new String(eventEmit.current_module_id).padStart(2,'0')
    if (eventEmit.current_module_id >= 10) id = eventEmit.current_module_id.toString(16).padStart(2,'0')
    // 不能超过10个
    let hex = generateCrcHex(id)
    let hexBuffer: Buffer = Buffer.from(hex,'hex')
    serialport.write(hexBuffer)
    eventEmit.current_module_id++
}

export default eventEmit
